<template>
  <span class="tools-li">
    <transition name="el-zoom-in-center">
      <div v-show="isShow" :key="tooltipId" ref="customAlert" class="custom-demo " transiton="el-zoom-in-center">
        <div style="width: 100%;height: 100%">
          <div class="custom-query">
            <input placeholder="快速探索 Tsinghua University">
            <div class="query-button"><i class="el-icon-search" /></div>
          </div>
        </div>
      </div>
    </transition>
    <slot name="reference" />
  </span>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util';
export default {
  name: 'PortalUserCommon',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      isShow: false
    };
  },
  computed: {
    tooltipId() {
      return `el-popover-${generateId()}`;
    }
  },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose);
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose);
    }
  },
  methods: {
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    },
    triggerOpen() {
      this.isShow = true;
      this.$emit('callBack', this.isShow);
    },
    triggerClose() {
      this.isShow = false;
      this.$emit('callBack', this.isShow);
    }
  }
};
</script>

<style scoped>
.custom-demo{
  position: absolute;
  width: 380px;
  left: -380px;
  height: 68px;
  top:0px;
  border-radius: 4px 0px 0px 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  background-color: #e3e3e3;
  color: #ffffff;
  padding: 10px 10px;
  box-sizing: border-box;
  font-size: 14px;
  font-weight: 400;
}
.custom-query {
  height: 40px;
  width: 100%;
  background-color: #ffffff;
  border-radius: 20px;
  padding: 0px 40px 0px 15px;
  box-sizing: border-box;
  line-height: 40px;
  position: relative;
}
.custom-query input {
  width: 100%;
  height: 40px;
  border: none;
  outline: none;
}
.query-button {
  position: absolute;
  right: 0px;
  top: 0px;
  width: 40px;
  height: 40px;
  line-height: 40px;
  cursor: pointer;
  color: #9b9b9b;
}
</style>
